package com.example.b_308.software.view.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RadioGroup;

import com.example.b_308.software.R;

import com.example.b_308.software.comment.view.title.util.Globel;
import com.example.b_308.software.controller.user.UserController;
import com.example.b_308.software.model.Company;
import com.example.b_308.software.model.Staff;
import com.example.b_308.software.model.base.BaseModel;
import com.example.b_308.software.service.IUserView;
import com.example.b_308.software.view.BaseFragment;
import com.example.b_308.software.view.WorkPageAdapter;
import com.example.b_308.software.view.WorkViewPage;
import com.example.b_308.software.view.base.BaseActivity;
import com.example.b_308.software.view.main.register.MessageFullFragment;
import com.example.b_308.software.view.main.register.RegSuccessFragment;
import com.example.b_308.software.view.main.register.VerifyFragment;
import com.example.b_308.software.view.main.register.VerifyNewFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by B-308 on 2017/11/29.
 */
public class RegisterActivity extends BaseActivity implements IUserView{
    WorkViewPage workViewPagerRegister;
    RadioGroup radioGroup;
    List<BaseFragment> fragmentsRegister=new ArrayList<>();
    int current=0;
    UserController userController;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        workViewPagerRegister = (WorkViewPage)findViewById(R.id.work_view_page_register);
        radioGroup= (RadioGroup) findViewById(R.id.rg_bottom_register);
       getTitleBar().setTitle(Globel.getString(R.string.register));
        getTitleBar().getTv_setting().setVisibility(View.GONE);
        fragmentsRegister.add(new VerifyFragment().setTitle("手机验证"));
        fragmentsRegister.add(new VerifyNewFragment().setTitle("手机验证"));
        fragmentsRegister.add(new MessageFullFragment().setTitle("信息完善"));
        fragmentsRegister.add(new MessageFullFragment().setTitle("信息完善"));
        fragmentsRegister.add(new RegSuccessFragment().setTitle("注册成功"));
        workViewPagerRegister.setAdapter(new WorkPageAdapter(getSupportFragmentManager(),fragmentsRegister));
      workViewPagerRegister.setCurrentItem(current);
    }
    Company company;
    Staff staff;
    public <T>void  next(BaseFragment baseFragment,T t){
        int index=fragmentsRegister.indexOf(baseFragment);
        if (null==company) company=new Company();
        if(null==staff) staff=new Staff();
        switch (index){
            case 0:
                //获取验证码--rg与vp的变化在获取验证码成功的时候处理
                if(t instanceof String){
                    staff.setPhone((String) t);
                    //获取验证码
                    userController.getCode(staff.getPhone());
                }
                break;
            case 1:
                //设置密码 当密码格式正确且两次输入相同的时候，设置staff的password属性为参数值，
                //并且设置rg第二个button选中，vp的第三个元素选中
                if(t instanceof String){
                    staff.put("password",t);
                    workViewPagerRegister.setCurrentItem(2);
                    radioGroup.getChildAt(1).setSelected(true);
                }
                break;
            case 2:
                //完善公司信息，循环迭代公司对象属性存放的map,完成过后设置vp第四个元素选中，rg不变
                if(t instanceof Company){
                    Map<String,Object>map=((Company) t).getAttrs();
                    for (Map.Entry<String ,Object>entry:map.entrySet()){
                        staff.put("company_"+entry.getKey(),entry.getValue());
                    }workViewPagerRegister.setCurrentItem(3);
                }
                break;
            case 3:
                if(t instanceof Staff){
                    staff.putAttrs((BaseModel) t);
                    userController.register(company,staff);
                }
                break;
            case 4:
                //注册成功点击体验回到登录界面并且销毁当前界面
                 tend2(LoginActivity.class,t,null);
                finish();
                break;

        }
    }
    @Override
    public void fail(String message) {

    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess(Staff staff) {
        //rg第三个元素设置为选中
        radioGroup.getChildAt(2).setSelected(true);
        //vp第五个元素为选中
        workViewPagerRegister.setCurrentItem(4);

    }
    @Override
    public void getCodeSuccess() {
        //rg不变，vp第二个元素选中
        workViewPagerRegister.setCurrentItem(1);

    }
}
